package cn.bellychang.ood.atmsimulator.transaction;

import cn.bellychang.ood.atmsimulator.Atm;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;

/**
 * @author ChangLiang
 * @date 2020/5/21
 */
public class TransferTx implements Transaction {

    private String fromCardNo;

    private Money userMoney;

    private String toCardNo;

    @Override
    public boolean preProcess(Atm atm) {

        this.fromCardNo = atm.getCardNo();

        atm.display("please input transfer to cardNo");
        toCardNo = atm.readInput();

        atm.display("please input transfer money amount");
        String moneyAmount = atm.readInput();
        // check atm has enough money
        userMoney = Money.of(CurrencyUnit.of("CNY"), Double.valueOf(moneyAmount));

        return true;
    }

    @Override
    public void doTransaction(Atm atm) {
        String bankResult = atm.getBankProxy().doTransfer(this);
        atm.display(bankResult);
    }

    @Override
    public boolean postProcess(Atm atm) {
        return false;
    }

    @Override
    public String toString() {
        return "TransferTx{" +
                "fromCardNo='" + fromCardNo + '\'' +
                ", userMoney=" + userMoney +
                ", toCardNo='" + toCardNo + '\'' +
                '}';
    }
}
